package com.zte.market.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zte.market.model.Custom;
import com.zte.market.model.MarketOrderCondition;
import com.zte.market.model.MarketOrders;
import com.zte.market.service.CustomService;
import com.zte.market.service.MarketOrdersService;
import com.zte.sys.login.model.LUsers;
import com.zte.util.OperatorLog;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 营销模块订单功能
 */
@RestController
@RequestMapping("/marketOrders")
public class MarketOrdersAction {
    @Autowired
    private MarketOrdersService ordersService;
    @Autowired
    private CustomService customService;
    @Autowired
    private RedisTemplate redisTemplate;
    //获取分页信息
    @PostMapping
    @OperatorLog(operate="多条件查询订单分页", module="订购单管理")
    public ResultMessage pageCustomByCondition(@RequestBody MarketOrderCondition condition) {
        int pageSize = condition.getPageSize();
        int pageNum = condition.getPageNum();
        if (pageSize <= 0) pageSize = 5;
        if (pageNum <= 0) pageNum = 1;
        PageModel<MarketOrders> pageModel = new PageModel<>();
        pageModel.setPageNum(pageNum);
        pageModel.setPageSize(pageSize);
        return new ResultMessage(200, true, "scuess", ordersService.pageOrdersInfoByCondition(pageModel, condition));
    }
    //添加订单
    @OperatorLog(operate="添加订单", module="订购单管理")
    @PostMapping("/addOrders")
    public ResultMessage addOrders(@RequestBody MarketOrders marketOrders){
        //查看用户名字是否有用
        Custom custom=customService.getOne(new LambdaQueryWrapper<Custom>().select(Custom::getCustomid,Custom::getStatus,Custom::getServiceid).eq(Custom::getCustomname,marketOrders.getCustomName()));
        LUsers lUsers=(LUsers) redisTemplate.opsForValue().get(String.valueOf(marketOrders.getOperatorid()));
        if(custom!=null) {
            if(custom.getStatus()==0){
                return new ResultMessage(400,false,"客户已注销",null);
            }
            if(lUsers.getJobId()!=1&&custom.getServiceid()!=marketOrders.getOperatorid()){
                return new ResultMessage(400,false,"你不是该客户服务人员",null);
            }
            marketOrders.setCustomid(custom.getCustomid());
            if (ordersService.addOrders(marketOrders)) {
                return new ResultMessage(200, true, "添加成功", null);
            } else
                return new ResultMessage(400, false, "添加失败", null);
        }else {
            return new ResultMessage(400,false,"客户不存在",null);
        }
    }
    //提交审核
    @PutMapping("/{userId}/{orderId}")
    @OperatorLog(operate="确定审核人员", module="订购单管理")
    public ResultMessage submitCheck(@RequestBody MarketOrderCondition condition, @PathVariable Integer userId,@PathVariable String orderId ){
       boolean success=ordersService.update(new LambdaUpdateWrapper<MarketOrders>().eq(MarketOrders::getOrderid,orderId)
                .set(MarketOrders::getStatus,2)
                .set(MarketOrders::getCheckid,userId));
       if(success){
           return pageCustomByCondition(condition);
       }else
        return new ResultMessage(400,false,"提交失败",null);
    }
    //删除订单
    @PostMapping ("/{orderId}")
    @OperatorLog(operate="删除订单", module="订购单管理")
    public ResultMessage delOrderInfo(@PathVariable String orderId ,@RequestBody MarketOrderCondition condition){
        MarketOrders marketOrders=ordersService.getOne(new LambdaQueryWrapper<MarketOrders>().select(MarketOrders::getStatus).eq(MarketOrders::getOrderid,orderId));
        //如果状态为未提交状态，则无法删除
        if(marketOrders.getStatus()!=1){
            return new ResultMessage(400,false,"不处于未提交状态无法删除",null);
        }else {
            ordersService.remove(new LambdaQueryWrapper<MarketOrders>().eq(MarketOrders::getOrderid,orderId));
            return pageCustomByCondition(condition);
        }
    }
    //更新订单
    @PutMapping
    @OperatorLog(operate="更新订单", module="订购单管理")
    public ResultMessage updateOrderInfo(@RequestBody MarketOrders marketOrders){
        //查看用户名字是否有用
        Custom custom=customService.getOne(new LambdaQueryWrapper<Custom>().select(Custom::getCustomid).eq(Custom::getCustomname,marketOrders.getCustomName()));
        if(custom!=null) {
            marketOrders.setCustomid(custom.getCustomid());
            if (ordersService.updateOrder(marketOrders)) {
                return new ResultMessage(200, true, "修改成功", null);
            } else
                return new ResultMessage(400, false, "修改失败", null);
        }else {
            return new ResultMessage(400,false,"客户不存在",null);
        }
    }
    //获取订单数据
    @GetMapping("/{orderId}")
    @OperatorLog(operate="获取订单详情", module="订购单管理")
    public ResultMessage getOrderInfoById(@PathVariable String orderId){
        return new ResultMessage(200,true,"success",ordersService.getOrderInfoById(orderId));
    }
}
